package com.itfitness.workmanagerdemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.work.Data;
import androidx.work.OneTimeWorkRequest;
import androidx.work.PeriodicWorkRequest;
import androidx.work.WorkInfo;
import androidx.work.WorkManager;
import androidx.work.WorkRequest;
import androidx.work.Worker;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    /**
     * 简单任务
     * @param view
     */
    public void simpleWork(View view) {
        OneTimeWorkRequest oneTimeWorkRequest =
                new OneTimeWorkRequest.Builder(SimpleWork.class)
                        .setInitialDelay(2, TimeUnit.SECONDS)
                        .build();
        WorkManager.getInstance(this).enqueue(oneTimeWorkRequest);
    }

    public void transferDataWork(View view) {
        Data data = new Data.Builder()
                .putString("data","Activity的数据").build();
        OneTimeWorkRequest oneTimeWorkRequest =
                new OneTimeWorkRequest.Builder(TransferDataWork.class)
                        .setInputData(data)
                        .build();
        WorkManager.getInstance(this).enqueue(oneTimeWorkRequest);
        //监听Worker回传的数据
        WorkManager.getInstance(this)
                .getWorkInfoByIdLiveData(oneTimeWorkRequest.getId())
                .observe(this, new Observer<WorkInfo>() {
                    @Override
                    public void onChanged(WorkInfo workInfo) {
                        if(workInfo.getState().isFinished()){
                            Data outputData = workInfo.getOutputData();
                            Log.e("传递数据任务",outputData.getString("data"));
                        }
                    }
                });
    }

    /**
     * 任务链
     * @param view
     */
    public void moreWork(View view) {
        ArrayList<OneTimeWorkRequest> workerRequests = new ArrayList();
        //任务1
        OneTimeWorkRequest oneTimeWorkRequest1 =
                new OneTimeWorkRequest.Builder(MoreWork.class)
                        .setInputData(new Data.Builder().putString("data","任务1").build())
                        .build();
        //任务2
        OneTimeWorkRequest oneTimeWorkRequest2 =
                new OneTimeWorkRequest.Builder(MoreWork.class)
                        .setInputData(new Data.Builder().putString("data","任务2").build())
                        .build();
        workerRequests.add(oneTimeWorkRequest1);
        workerRequests.add(oneTimeWorkRequest2);

        //任务3
        OneTimeWorkRequest oneTimeWorkRequest3 =
                new OneTimeWorkRequest.Builder(MoreWork.class)
                        .setInputData(new Data.Builder().putString("data","任务3").build())
                        .build();
        WorkManager.getInstance(this)
                .beginWith(workerRequests)//先执行的任务
                .then(oneTimeWorkRequest3)//后执行的任务
                .enqueue();
    }

    /**
     * 周期性任务
     * @param view
     */
    public void periodicTask(View view) {
        PeriodicWorkRequest periodicWorkRequest =
                new PeriodicWorkRequest.Builder(PeriodicWork.class,15,TimeUnit.MINUTES)
                        .build();
        WorkManager.getInstance(this)
                .enqueue(periodicWorkRequest);
    }
}